package com.javasee.common.dbsharding.mybatis.typeHandler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.*;


@MappedTypes({Long.class})
@MappedJdbcTypes({JdbcType.TIMESTAMP})
public class LongTimestampTypeHandler extends BaseTypeHandler<Long> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Long parameter, JdbcType jdbcType) throws SQLException {
        Timestamp  time = new Timestamp (parameter);
        ps.setTimestamp(i, time);
    }

    @Override
    public Long getNullableResult(ResultSet rs, String columnName) throws SQLException {
        Timestamp  time = rs.getTimestamp(columnName);
        if (time != null) {
            return time.getTime();
        }
        return null;
    }

    @Override
    public Long getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        Timestamp  time = rs.getTimestamp(columnIndex);
        if (time != null) {
            return time.getTime();
        }
        return null;
    }

    @Override
    public Long getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        Timestamp  time = cs.getTimestamp(columnIndex);
        if (time != null) {
            return time.getTime();
        }
        return null;
    }
}
